package pers.chao.tool.math;

import java.util.Random;

/**
 * Description: 字母、数字随机字符串
 *
 * @author W.Chao
 * @date 2020/11/8 15:40
 **/
public final class RandomUtil {

	private static final Random random = new Random();
	private static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String NUMBER_CHAR = "0123456789";

	private RandomUtil() {
	}

	public static Random getRandom() {
		return random;
	}

	public static String randomChar(int returnRandomLength) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < returnRandomLength; i++) {
			sb.append(ALL_CHAR.charAt(random.nextInt(ALL_CHAR.length())));
		}
		return sb.toString();
	}

	public static String random8Char() {
		return randomChar(8);
	}

	public static String random16Char() {
		return randomChar(16);
	}

	public static String random32Char() {
		return randomChar(32);
	}

	public static String random64Char() {
		return randomChar(64);
	}

	public static String random128Char() {
		return randomChar(128);
	}

	public static String random256Char() {
		return randomChar(256);
	}

	public static String random512Char() {
		return randomChar(512);
	}

	/**
	 * 获取定长的随机数，包含大小写字母
	 *
	 * @param returnRandomLength 随机数长度
	 * @return
	 */
	public static String randomLowerAndUpper(int returnRandomLength) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < returnRandomLength; i++) {
			sb.append(LETTER_CHAR.charAt(random.nextInt(LETTER_CHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 获取定长的随机数，只包含小写字母
	 *
	 * @param returnRandomLength 随机数长度
	 * @return
	 */
	public static String randomOnlyLower(int returnRandomLength) {
		return randomLowerAndUpper(returnRandomLength).toLowerCase();
	}

	/**
	 * 获取定长的随机数，只包含大写字母
	 *
	 * @param returnRandomLength 随机数长度
	 * @return
	 */
	public static String randomOnlyUpper(int returnRandomLength) {
		return randomLowerAndUpper(returnRandomLength).toUpperCase();
	}

	/**
	 * 获取定长的随机数，只包含数字
	 *
	 * @param returnRandomLength 随机数长度
	 * @return
	 */
	public static String randomOnlyNumber(int returnRandomLength) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < returnRandomLength; i++) {
			sb.append(NUMBER_CHAR.charAt(random.nextInt(NUMBER_CHAR.length())));
		}
		return sb.toString();
	}
}
